import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UserService } from 'src/app/core/service/user.service';

@Component({
  selector: 'app-add-org-user',
  templateUrl: './add-org-user.component.html',
  styleUrls: ['./add-org-user.component.scss']
})
export class AddOrgUserComponent implements OnInit {
  orgUserForm: FormGroup;
  isHidden = false;
  /* radioValue = '0';
  checked = true; */
  info = {};

  nameFlag = '';
  msgFlag = '';

  constructor(
    private modal: NzModalRef,
    private message: NzMessageService,
    private userService: UserService,
    private fb: FormBuilder
  ) {
    this.orgUserForm = fb.group({
      orgName: ['', Validators.required],
      /* domainName: ['', Validators.required],
      whichOne: ['0', Validators.required], */
      email: ['', Validators.compose([Validators.required, Validators.email])],
      firstName: ['', Validators.required],
      lastName: ['', Validators.required]
    });
  }

  change(value: string[]): void {
    if (value.length > 0) {
      this.isHidden = true;
    } else {
      this.isHidden = false;
    }
    console.log(value);
  }

  /**
   * 新增企业用户
   */
  saveOrgUser() {
    console.log('提交', this.orgUserForm.controls.orgName.value);
    // tslint:disable-next-line:forin
    for (const c in this.orgUserForm.controls) {
      this.orgUserForm.controls[c].markAsTouched();
    }

    if (this.orgUserForm.valid) {
      this.info = {
        orgName: this.orgUserForm.controls.orgName.value,
        email: this.orgUserForm.controls.email.value,
        firstName: this.orgUserForm.controls.firstName.value,
        lastName: this.orgUserForm.controls.lastName.value
      };
      this.addService(this.info);
    } else {
      this.message.error('表单校验失败', { nzDuration: 10000 });
    }
  }

  private addService(info: any) {
    this.userService.saveOrgUser(this.info)
      .subscribe((OrgUserData) => {
        console.log(OrgUserData);
        if (OrgUserData.status) {
          this.message.success('添加成功!', {
            nzDuration: 10000
          });
          this.modal.destroy();
        } else {
          this.message.error('添加失败!请检查后重新填写', {
            nzDuration: 10000
          });
        }
      });
  }

  destroyModal(): void {
    this.modal.destroy({ data: 'this the result data' });
  }

  /**
   * 判断企业名称是否重复
   */
  isOrgNameRepeat() {
    console.log('=== Janine.isOrgNameRepeat().orgName ===>', this.orgUserForm.controls.orgName.value);
    if (this.orgUserForm.controls.orgName.value) {
      this.userService.isOrgNameRepeat(this.orgUserForm.controls.orgName.value)
        .subscribe((data) => {
          console.log(data);
          if (data.status === 3) { // 重复
            this.nameFlag = 'error';
          }
          if (data.status === 1) { // 可用
            this.nameFlag = 'success';
          }
        });
    }
  }


  /**
   * 判断邮箱是否重复
   */
  isEmailRepeat() {
    console.log('=== Janine.isEmailRepeat().email ===>', this.orgUserForm.controls.email.value);
    if (this.orgUserForm.controls.email.value) {
      this.userService.isEmailRepeat(this.orgUserForm.controls.email.value)
        .subscribe((data) => {
          console.log(data);
          if (data.status === 3) { // 重复
            this.msgFlag = 'error';
          }
          if (data.status === 1) { // 可用
            this.msgFlag = 'success';
          }
        });
    }
  }

  ngOnInit() {
  }

}
